// Promise是一个构造函数
//  构造函数有两个属性。PromiseState   PromiseResult

function Promise(executor){
    this.PromiseState = 'pending';
    this.PromiseResult = undefined;
    // 改变this指向方案一：
    // let _this = this;
    // function resolve(value){
    //     // 1. state: pending ---> fulfilled
    //     // 2. result： undefined ---> ok
    //     _this.PromiseState = 'fulfilled';
    //     _this.PromiseResult = value;
    //     console.log('this',_this);
    // }
    // 改变this方案二：箭头函数
    let resolve = (value)=>{
        // 状态必须得是pending才能执行以下代码
        if(this.PromiseState !== 'pending') return;
        this.PromiseState = 'fulfilled';
        this.PromiseResult = value;
    }
    let reject = (reason)=>{
        // 状态必须得是pending才能执行以下代码
        if(this.PromiseState !== 'pending') return;
        this.PromiseState = 'rejected';
        this.PromiseResult = reason;
    }
    try{
        executor(resolve,reject);
    }catch(e){
        // console.log(e);
        // this.PromiseState = 'rejected'
        // this.PromiseResult = e;
        reject(e);
    }
    
}